﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace dbproj
{
    class Dao
    {
        /// <summary>
        /// 链接数据库
        /// </summary>
        /// <returns></returns>
        public SqlConnection connect()
        {
            string str = @"Data Source=iSODA;Initial Catalog=Demo;Integrated Security=True;Encrypt=False"; //链接到数据库
            SqlConnection sc = new SqlConnection(str); //生成链接对象
            sc.Open(); //打开链接
            return sc;
        }

        /// <summary>
        /// 接收SQL语句，进行处理
        /// </summary>
        /// <param name="sql">要使用的SQL语句</param>
        /// <returns></returns>
        public SqlCommand command(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, connect());
            return cmd;
        }
        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>受影响的行数</returns>
        public int Execute(string sql)
        {
            return command(sql).ExecuteNonQuery();
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>SqlDataReader对象，包含select到的数据</returns>
        public SqlDataReader read(string sql)
        {
            return command(sql).ExecuteReader();
        }
    }
}
